package net.vanchu.mortgagecalculator.mortgage.calculate.single;

import android.text.TextUtils;

import net.vanchu.mortgagecalculator.common.commonView.component.WritingOptionLayout;
import net.vanchu.mortgagecalculator.common.config.LoanRate;
import net.vanchu.mortgagecalculator.common.config.LoanWay;
import net.vanchu.mortgagecalculator.common.config.OptionsProvider;
import net.vanchu.mortgagecalculator.common.config.TestConfigProvider;
import net.vanchu.mortgagecalculator.mortgage.calculate.CalculateResultListener;
import net.vanchu.mortgagecalculator.mortgage.calculate.CalculatorHelper;
import net.vanchu.mortgagecalculator.mortgage.calculate.OnLoanWaySwitchListener;
import net.vanchu.mortgagecalculator.mortgage.calculate.LoanNumberInputValidator;

import java.text.DecimalFormat;
import java.util.Locale;

/**
 * Created by Granton on 2016/12/21.
 */
public class CalSingleLoanPresenter implements
        CalSingleLoanContract.Presenter, WritingOptionLayout.InputStateListener {

    private CalSingleLoanContract.View view;

    private SingleLoanWayInputData data;

    private OnLoanWaySwitchListener onLoanWaySwitchListener;

    private CalculateResultListener calculateResultListener;

    public CalSingleLoanPresenter(CalSingleLoanContract.View view,
                                  SingleLoanWayInputData data,
                                  OnLoanWaySwitchListener onLoanWaySwitchListener,
                                  CalculateResultListener calculateResultListener) {
        this.view = view;
        this.data = data;
        this.onLoanWaySwitchListener = onLoanWaySwitchListener;
        this.calculateResultListener = calculateResultListener;
    }

    @Override
    public void chooseLoanWay(LoanWay loanWay) {
        if (loanWay == LoanWay.wayMix) {
            onLoanWaySwitchListener.onLoanWaySwitch(loanWay);
            return;
        }
        view.showLoanWay(OptionsProvider.convertLoanWayToStringShow(loanWay));
        boolean reset = data.getCurrentLoanWay() == loanWay;
        data.setCurrentLoanWay(loanWay);
        if (reset) {
            data.setRate(0);
            data.setDiscount(0);
            view.resetLoanRates();
            view.resetLoanYears();
        }
        checkInputSatisfied();
    }

    @Override
    public void preChooseLoanWay() {
        view.showLoanWays();
    }

    @Override
    public void chooseLoanYear(int year) {
        String yearExpress = String.format(Locale.getDefault(), "%d年", year);
        view.showLoanYear(yearExpress);
        boolean reset = data.getYear() == year;
        data.setYear(year);
        if (reset) {
            data.setRate(0);
            data.setDiscount(0);
            view.resetLoanRates();
        }
        checkInputSatisfied();
    }

    @Override
    public void preChooseLoanYear() {
        if (data.getCurrentLoanWay() == null) {
            view.tipError("请先选择贷款方式");
            return;
        }
        view.showLoanYears();
    }

    @Override
    public WritingOptionLayout.InputStateListener asInputStateListener() {
        return this;
    }

    @Override
    public void chooseLoanRates(LoanRate loanRate, float discount) {
        String loanRateExpress;
        float rate = loanRate.getValue(data.getCurrentLoanWay(), data.getYear());
        if (discount == 1) {
            loanRateExpress = rate + "%";
        } else {
            DecimalFormat formatToTwoDecimal = new DecimalFormat("#.##");
            String discountStr = TestConfigProvider.extractDisCountName(
                    formatToTwoDecimal, discount
            );
            loanRateExpress = rate + "%" + " x " + discountStr;
        }
        view.showLoanRate(loanRateExpress);
        data.setDiscount(discount);
        data.setRate(rate);
        checkInputSatisfied();
    }

    @Override
    public void preChooseLoanRates() {
        if (data.getCurrentLoanWay() == null) {
            view.tipError("请先选择贷款方式");
            return;
        }
        if (data.getYear() == 0) {
            view.tipError("请先选择贷款年限");
            return;
        }
        view.showLoanRates();
    }

    @Override
    public void applyCalculate() {
        calculateResultListener.onSingleWayCalculateResult(
                CalculatorHelper.getInstance().calSingle(data)
        );
    }

    private void checkInputSatisfied() {
        if (data.inputSatisfied()) {
            view.showEnableButton();
        } else {
            view.showDisableButton();
        }
    }

    @Override
    public void onInputValid(int id, String validText) {
        if (TextUtils.isEmpty(validText)) {
            // 当用户输入数字又清空输入框内的文字之后触发这个分支
            data.setValue(0);
        } else {
            data.setValue(Float.parseFloat(validText));
        }
        checkInputSatisfied();
    }

    @Override
    public void onInputInvalid(String errorMsg) {
        view.tipError(errorMsg);
    }

    @Override
    public WritingOptionLayout.InputValidator asInputValidator() {
        return new LoanNumberInputValidator(new String[1]);
    }
}